package com.grwx.omc.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grwx.omc.constant.Constant;
import com.grwx.omc.entity.ResponseData;
import com.grwx.omc.exception.ValidationException;
import com.grwx.omc.admin.entity.SysLookupDetail;
import com.grwx.omc.admin.service.ISysLookupDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/sys_lookup_detail")
public class SysLookupDetailController {

    @Autowired
    private ISysLookupDetailService iSysLookupDetailService;

    @ApiOperation(value = "分页查询字典分类")
    @PostMapping("/search/{page}/{size}")
    public ResponseData page(@RequestBody(required = false) SysLookupDetail sysLookupDetail, @PathVariable("page") int page,
                             @PathVariable("size")  int size) {
        Page<SysLookupDetail> sysLookupDetailPage = new Page<>(page, size);
        IPage<SysLookupDetail> result = iSysLookupDetailService.page(sysLookupDetailPage, sysLookupDetail);
        return ResponseData.success(result);
    }


    @ApiOperation(value = "新增字典数据")
    @PostMapping()
    public ResponseData insert(@RequestBody SysLookupDetail sysLookupDetail) {
        iSysLookupDetailService.addOrUpdate(sysLookupDetail);
        return ResponseData.success(Constant.MsgCode.INSERT_SUCCESS);
    }

    @ApiOperation(value = "修改字典数据")
    @PutMapping()
    public ResponseData update(@RequestBody SysLookupDetail sysLookupDetail) {
        iSysLookupDetailService.addOrUpdate(sysLookupDetail);
        return ResponseData.success(Constant.MsgCode.UPDATE_SUCCESS);
    }

    @ApiOperation(value = "删除字典数据")
    @DeleteMapping("/{id}")
    public ResponseData del(@PathVariable("id") Long id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new ValidationException(Constant.MsgCode.EMPTY_DELETE);
        }
        iSysLookupDetailService.removeById(id);
        return ResponseData.success(Constant.MsgCode.DELETE_SUCCESS);
    }

    @ApiOperation(value = "批量删除字典数据")
    @DeleteMapping("/batch")
    public ResponseData batchDel(Long[] ids) {
        if (ObjectUtils.isEmpty(ids)) {
            throw new ValidationException(Constant.MsgCode.EMPTY_DELETE);
        }
        iSysLookupDetailService.removeByIds(Arrays.asList(ids));
        return ResponseData.success(Constant.MsgCode.DELETE_SUCCESS);
    }




}
